package com.zx.controller.dj;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.zx.model.dj.StockMaster;
import com.zx.service.dj.StockManageService;
import com.zx.utils.web.Pagination;

/**
 * 
 * @author zx
 *
 */
@Controller
@RequestMapping("store")
public class StockManageController {

    @Autowired
    StockManageService stockManageService;

    @RequestMapping("toAddGoods")
    public String toAddGoods(Model model) {
        return "dj/AddGoods";
    }

    /**
     * 出库列表显示
     * @param mod
     * @param pageIndex
     * @param pageSize
     * @param orderType
     * @return
     */
    @RequestMapping("toDeleteGoods.do")
    public String toDeleteGoods(Model mod
            ,@RequestParam(defaultValue="1", required=false)Integer pageIndex
            ,@RequestParam(defaultValue="10", required=false)Integer pageSize
            ,@RequestParam(defaultValue="0", required=false)int orderType
            ,@RequestParam(defaultValue="", required=false)String sbrand
            ,@RequestParam(defaultValue="", required=false)String smodel
            ,@RequestParam(defaultValue="", required=false)String scolor
            ,@RequestParam(defaultValue="", required=false)String ssize) {
        
        selMasterDatas(mod, pageIndex, pageSize, orderType,sbrand,smodel,scolor,ssize);
        mod.addAttribute("sbrand", sbrand);
        mod.addAttribute("smodel", smodel);
        mod.addAttribute("scolor", scolor);
        mod.addAttribute("ssize", ssize);
        
        
        return "dj/DeleteGoodsNew";
    }

    /**
     * 入库
     * 
     * @param model
     * @return
     */
    @RequestMapping("addGoods")
    public String addGoods(Model mod, 
            String brand, String model, String color, String size, Integer count, String price,
            String supplier, String arrivalTime) {

        try {
            stockManageService.addStockMaster(
                    brand, model, color, size, count, price, supplier, arrivalTime);
            mod.addAttribute("result", "1");
        } catch (Exception e) {
            mod.addAttribute("result", "0");
            e.printStackTrace();
        }
        
        return "dj/AddGoods";
    }
    
    /**
     * 出库
     * 
     * @param model
     * @return
     */
    @RequestMapping("DeleteGoods")
    @ResponseBody
    public String DeleteGoods(
            Model mod, Long mid, @RequestParam(defaultValue="0", required=true)Integer outCount, String price, String name,String outTime
            ) {
        
        JSONObject json = new JSONObject();
        try {
            // 出库
            stockManageService.deleteGoods(mid, outCount, price, name, outTime);
            json.put("result", "1");
        } catch (Exception e) {
            e.printStackTrace();
            json.put("result", "0");
        }

        return json.toString();
    }

    /**
     * 总仓库存
     * 
     * @param model
     * @return
     */
    @RequestMapping("selStockMasters")
    public String selStockMasters(Model mod, 
            @RequestParam(defaultValue="1", required=false)Integer pageIndex,
            @RequestParam(defaultValue="10", required=false)Integer pageSize, 
            @RequestParam(defaultValue="0", required=false)int orderType
            ,@RequestParam(defaultValue="", required=false)String sbrand
            ,@RequestParam(defaultValue="", required=false)String smodel
            ,@RequestParam(defaultValue="", required=false)String scolor
            ,@RequestParam(defaultValue="", required=false)String ssize) {

        selMasterDatas(mod, pageIndex, pageSize, orderType,sbrand,smodel,scolor,ssize);
        
        mod.addAttribute("sbrand", sbrand);
        mod.addAttribute("smodel", smodel);
        mod.addAttribute("scolor", scolor);
        mod.addAttribute("ssize", ssize);
        
        return "dj/Stock";
    }

    /**
     * 查询总仓数据
     * @param mod
     * @param pageIndex
     * @param pageSize
     * @param orderType
     */
    private void selMasterDatas(Model mod, Integer startIndex,Integer pageSize, int orderType,String sbrand,String smodel,String scolor,String ssiz) {
        try {
            
            Integer total = stockManageService.selStockMastersCount(sbrand,smodel,scolor,ssiz);
            List<StockMaster> rst = stockManageService.selStockMastersCondition(startIndex, pageSize, orderType,sbrand,smodel,scolor,ssiz);
            Pagination<StockMaster> pagination = new Pagination<>(startIndex, pageSize, total);
            pagination.setData(rst);
            mod.addAttribute("pagination", pagination);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    
    /**
     * 分仓库存
     * 
     * @param model
     * @return
     */
    @RequestMapping("selStockBranchs")
    public String selStockBranchs(Model mod, 
            @RequestParam(defaultValue="1", required=false)Integer pageIndex,
            @RequestParam(defaultValue="10", required=false)Integer pageSize, 
            @RequestParam(defaultValue="0", required=false)int orderType
            ,@RequestParam(defaultValue="", required=false)String sbrand
            ,@RequestParam(defaultValue="", required=false)String smodel
            ,@RequestParam(defaultValue="", required=false)String scolor
            ,@RequestParam(defaultValue="", required=false)String ssize
            ,@RequestParam(defaultValue="", required=false)String shopname
            ,@RequestParam(defaultValue="", required=false)String remindCount
            ) {

        try {
            Integer total = stockManageService.selStockBranchCount(sbrand,smodel,scolor,ssize,shopname,remindCount);
            List<StockMaster> rst = stockManageService.selStockBranchCondition(pageIndex, pageSize, orderType,sbrand,smodel,scolor,ssize,shopname,remindCount);
            Pagination<StockMaster> pagination = new Pagination<>(pageIndex, pageSize, total);
            pagination.setData(rst);
            mod.addAttribute("pagination", pagination);
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        mod.addAttribute("sbrand", sbrand);
        mod.addAttribute("smodel", smodel);
        mod.addAttribute("scolor", scolor);
        mod.addAttribute("ssize", ssize);
        mod.addAttribute("shopname", shopname);
        mod.addAttribute("remindCount", remindCount);
        
        return "dj/Remind";
    }
}